<?php
 
require_once "HTTP/Request.php";
 
function get_ticket($src, $dst, $date, $trains = array()) {
	
	$r = new Http_Request();
	$r->setMethod(HTTP_REQUEST_METHOD_GET);
	$r->setURL("http://711.ua/t2t_tickets");
	$r->addQueryString('transport_type', 2);
	$r->addQueryString('src', $src);
	$r->addQueryString('dst', $dst);
	$r->addQueryString('dt', $date);
	
	if (!PEAR::isError($r->sendRequest())) {
	     $response1 = $r->getResponseBody();
	     $cooka = $r->getResponseCookies();
	} else {
	     $response1 = "";
	}

	preg_match_all("/window.trips_\['[0-9]{1,2}'\]=\{guididx: '[0-9\-a-f]{36}'/", $response1, $lines);
	$train_info = array();
	foreach($lines[0] as $line){
		$train_info[] = array("UUID" => substr($line, -37, 36));
	}

	$dom = new domDocument;
	
	/** загружаем html в объект **/
    @$dom->loadHTML($response1);
    $dom->preserveWhiteSpace = false;
    
    /** элемент по тэгу **/
    $match = implode("|", $trains);
    
    foreach($train_info as $key=>$train){
		$temp = $dom->getElementById("row_".$key)->getElementsByTagName("td")->item(0)->nodeValue;
		if (preg_match("(".$match.")", $temp )) {
			$train_for_add[] = array(
				"ID" => $temp,
				"UUID" => $train_info[$key]["UUID"],
			);
		}
	}
	unset($dom);

    foreach($train_for_add as $key=>$value) {
	    $r->setURL("http://711.ua/sites/all/modules/gui_drupal_t2t/trip-info.php");
		$r->addQueryString('segment_id', $train_for_add[$key]["UUID"]);
		$r->addCookie($cooka[0]["name"], $cooka[0]["value"]);
		if (!PEAR::isError($r->sendRequest())) {
		     $response2 = $r->getResponseBody();
		     print_r($r->getResponseCookies());
		} else {
		     $response2 = "";
		}
	
	    $dom = new domDocument;
		
		/** загружаем html в объект **/
	    @$dom->loadHTML($response2);
	    $dom->preserveWhiteSpace = false;
	    
	    preg_match_all("/wagon_[0-9]{1,2}/", $response2, $lines);
	    
	    foreach($lines[0] as $line){
			$train_for_add[$key]["VAGONS"][] = array( 
				"NUM" => $dom->getElementById($line)->getElementsByTagName("td")->item(0)->nodeValue,
				"TYPE" => $dom->getElementById($line)->getElementsByTagName("td")->item(1)->nodeValue,
				"TOP" => $dom->getElementById($line)->getElementsByTagName("td")->item(2)->nodeValue,
				"BOTTOM" => $dom->getElementById($line)->getElementsByTagName("td")->item(3)->nodeValue,
				"B_TOP" => $dom->getElementById($line)->getElementsByTagName("td")->item(4)->nodeValue,
				"B_BOTTOM" => $dom->getElementById($line)->getElementsByTagName("td")->item(5)->nodeValue,
				"PRICE" => $dom->getElementById($line)->getElementsByTagName("td")->item(6)->nodeValue,
			);
		}
		unset($dom);
	}
   print_r($train_for_add);
}

get_ticket("22210710", "22200001", "2012-07-16", array("238", "206"));
?>

